const App = getApp()
const db = wx.cloud.database()
const _ = db.command
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    // 课程数组
    courselist: [],
    // 获取今天的课程
    dayCourse: [],
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    let data = JSON.parse(decodeURIComponent(options.passData));
    console.log('拿到传值回来的值:', data);
    if (data) {
      data.active = Number(data.active)
      that.setData({
        active: data.active,
      })
    }
    let UserInfo = App.cache.getUserInfo()
    console.log('获取用户信息:', UserInfo);
    delete UserInfo._id
    that.setData({
      UserInfo: UserInfo,
    })

    // 获取今日课程以及全部课程
    that.getMycourse()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that.getMycourse()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取我的课程
  getMycourse() {
    db.collection('course').where(_.or([{
          permissions: _.eq(that.data.UserInfo.permissions)
        },
        {
          lecturer: {
            account: _.eq(that.data.UserInfo.account)
          }
        },
        {
          courseMember: _.elemMatch({
            account: _.eq(that.data.UserInfo.account)
          })
        }
      ]))
      .get({
        success: function (res) {
          console.log(res.data)
          // 获取全部课程
          let course = res.data
          console.log('获取全部课程', course);
          // 获取当天的时间戳
          let today = new Date(new Date(new Date().toLocaleDateString()).getTime());
          today = Date.parse(today)
          console.log('当天零点的时间戳', today);
          // 获取当天23点59分59秒的时间戳 
          let tomorrow = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
          tomorrow = Date.parse(tomorrow)
          console.log('当天23.59.59的时间戳', tomorrow);
          console.log('当前的时间戳', App.Time.formatTime(Date.now()));
          let dayCourse = []
          // 筛选今天的课程
          course.forEach(item => {
            dayCourse = item.courseTime.filter(x => {
              return x.addTime < tomorrow && x.endTime - Date.now() > 0
            })

          });
          // 将过滤出来的课程做一个状态，还有多久上课，是否正在上课
          dayCourse.forEach(item => {
            item.addTime = item.addTime - Date.now()
            item.endTime = item.endTime - Date.now()
          })
          console.log('筛选今日的课程', dayCourse);
          that.setData({
            courselist: course,
            dayCourse: dayCourse
          })
        }
      })
  },

  addroute() {
    App.route.navTo('/pages/course/add/index')
  },
  details(e) {
    let path = e.currentTarget.dataset.path
    console.log('获取班级id', path);
    App.route.navTo('/pages/course/details/index', path)
  }
})